package com.gem.growingtree.controller;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gem.growingtree.model.Admin;
import com.gem.growingtree.service.AdminService;


@SuppressWarnings("serial")
@Controller
public class LoginController extends HttpServlet{
	@RequestMapping("/login")
	public ModelAndView addAdmin(HttpSession mySession,String userId,String adminName,String adminPass ,HttpServletRequest request,HttpServletResponse response){
	
		AdminService as = new AdminService();
		Admin admin = new Admin(userId,adminName,adminPass);
		try {
			if(as.check(admin)){	
				System.out.println("登录成功 ");
				mySession.setAttribute("userId", userId);
				mySession.setAttribute("loginName",adminName);
				mySession.setAttribute("loginPass",adminPass);
				return new ModelAndView("/index");
			}
			else{
				System.out.println("登录失败 ");
				String wrongPass = "密码错误，请重新输入密码";			
				return new ModelAndView("/login","wrongPass",wrongPass);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
